﻿using System;
using System.Net;
using System.Text;
using System.Windows;
using eLearning.Model;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Newtonsoft.Json;

namespace eLearning.DosenView
{
    public partial class MahasiswaDetailPage : PhoneApplicationPage
    {
        private WebClient webClient;
        private Mahasiswa mahasiswa;
        private Perkuliahan kuliah;

        public MahasiswaDetailPage()
        {
            InitializeComponent();
            listMahasiswaDetail.Items.Clear();
            this.Loaded += new RoutedEventHandler(RssItemPage_Loaded);
        }

        #region Event Handlers
        void RssItemPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (PhoneApplicationService.Current.State.ContainsKey(Config.KULIAH))
            {
                kuliah = (Perkuliahan)PhoneApplicationService.Current.State[Config.KULIAH];
                mahasiswa = kuliah.Mhs;
            }
            else if (PhoneApplicationService.Current.State.ContainsKey(Config.MHS))
            {
                mahasiswa = (Mahasiswa)PhoneApplicationService.Current.State[Config.MHS];
            }

            if (mahasiswa != null)
            {
                if (mahasiswa.Status == Mahasiswa.STATUS.JOIN || mahasiswa.Status == Mahasiswa.STATUS.REGISTER)
                    ApplicationBar.IsVisible = true;
                else
                    ApplicationBar.IsVisible = false;
                listMahasiswaDetail.Items.Add(mahasiswa);
            }
        }

        #endregion

        private void approve_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            try
            {
                if (e.Cancelled && e.Error != null)
                {
                    return;
                }
                else if (e.Result.Contains("approved") || e.Result.Contains("accepted"))
                {
                    NavigationService.GoBack();
                }
                MessageBox.Show("Message : " + e.Result);
            }
            catch (WebException ex)
            {
                MessageBox.Show("ERROR : " + ex.Message);
            }
        }

        private void buttonApprove_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Ijinkan pendaftaran mahasiswa?", "", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                string json = "";
                if (mahasiswa != null && mahasiswa.Status == Mahasiswa.STATUS.REGISTER)
                    json = JsonConvert.SerializeObject(mahasiswa);
                else if (kuliah != null && mahasiswa.Status == Mahasiswa.STATUS.JOIN)
                    json = JsonConvert.SerializeObject(kuliah);

                webClient = new WebClient();
                webClient.Encoding = Encoding.UTF8;
                webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
                webClient.Headers[HttpRequestHeader.ContentLength] = json.Length.ToString();

                webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(approve_UploadStringCompleted);

                if (mahasiswa != null && mahasiswa.Status == Mahasiswa.STATUS.REGISTER)
                    webClient.UploadStringAsync(new Uri(Config.Instance.DOSEN_SERVICE_URL() + Config.REG_APPROVE_BYNIM_KEY, UriKind.Absolute), json);

                else if (kuliah != null && mahasiswa.Status == Mahasiswa.STATUS.JOIN)
                    webClient.UploadStringAsync(new Uri(Config.Instance.PERKULIAHAN_SERVICE_URL() + Config.ACCEPT_MEMBER_BYNIM_KEY, UriKind.Absolute), json);
            }
        }

        private void reject_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            try
            {
                if (e.Cancelled && e.Error != null)
                {
                    return;
                }
                else if (e.Result.Contains("rejected"))
                {
                    NavigationService.GoBack();
                }
                MessageBox.Show("Message : " + e.Result);
            }
            catch (WebException ex)
            {
                MessageBox.Show("ERROR : " + ex.Message);
            }
        }
        
        private void buttonReject_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Batalkan pendaftaran mahasiswa?", "", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                string json = "";
                if (mahasiswa != null && mahasiswa.Status == Mahasiswa.STATUS.REGISTER)
                    json = JsonConvert.SerializeObject(mahasiswa);
                else if (kuliah != null && mahasiswa.Status == Mahasiswa.STATUS.JOIN)
                    json = JsonConvert.SerializeObject(kuliah);

                webClient = new WebClient();
                webClient.Encoding = Encoding.UTF8;
                webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
                webClient.Headers[HttpRequestHeader.ContentLength] = json.Length.ToString();

                webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(reject_UploadStringCompleted);

                if (mahasiswa != null && mahasiswa.Status == Mahasiswa.STATUS.REGISTER)
                    webClient.UploadStringAsync(new Uri(Config.Instance.DOSEN_SERVICE_URL() + Config.REG_REJECT_BYNIM_KEY, UriKind.Absolute), json);

                else if (kuliah != null && mahasiswa.Status == Mahasiswa.STATUS.JOIN)
                    webClient.UploadStringAsync(new Uri(Config.Instance.PERKULIAHAN_SERVICE_URL() + Config.REJECT_MEMBER_BYNIM_KEY, UriKind.Absolute), json);
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }

    }
}